/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.ware;

import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.SysSetPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.ISysSetConnector;
import com.itttq.gshop.generic.connector.IWareInfoConnector;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.generic.util.Constants.WareStatus;

@RestController
@RequestMapping("ware/info")
public class WareInfoController extends GenericController {
	private static final long serialVersionUID = 152159243572735133L;
	@Autowired
	private IWareInfoConnector wareInfoConnector;
	@Autowired
	private ISysSetConnector sysSetConnector;
	
	/**
	 * 根据条件查询WareInfoPo对象，并分页
	 * 
	 * @param wareInfo
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("infos.html")
	public ModelAndView getWareInfos(WareInfoPo wareInfo, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/ware/infos");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		wareInfo.setStart(ppt.getStart());
		wareInfo.setSize(ppt.getSize());
		ResultPageTo<WareInfoPo> rpt = wareInfoConnector.getWareInfos(wareInfo);
		this.getPageMv("infos", mv, rpt);
		mv.addObject("info", wareInfo);
		getAllCategories(mv);
		getAllBrands(mv);
		return mv;
	}
	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addWareInfo() {
		ModelAndView mv = new ModelAndView("/ware/infoAdd");
		getAllCategories(mv);
		getAllBrands(mv);
		// 获取系统业务设置，主要是积分部分 TODO GXG 考虑一次获取
		SysSetPo set = sysSetConnector.getSysSet(Constants.SCORE_EXCH_RATE);
		mv.addObject(Constants.SCORE_EXCH_RATE, set == null ? 0 : set.getValue());
		set = sysSetConnector.getSysSet(Constants.SCORE_GIVE_RATE);
		mv.addObject(Constants.SCORE_GIVE_RATE, set == null ? 0 : set.getValue());
		set = sysSetConnector.getSysSet(Constants.SCORE_PROMOTE_RATE);
		mv.addObject(Constants.SCORE_PROMOTE_RATE, set == null ? 0 : set.getValue());
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewWareInfo(@PathVariable("id") Integer id) {
		ModelAndView mv = addWareInfo();
		if(id != null) {
			mv.addObject("info", wareInfoConnector.getWareInfo(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editWareInfo(@PathVariable("id") Integer id) {
		ModelAndView mv = viewWareInfo(id);
		mv.addObject("op", "edit");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("copy/{id}.html")
	public ModelAndView copyWareInfo(@PathVariable("id") Integer id) {
		ModelAndView mv = addWareInfo();
		WareInfoPo wareInfo = wareInfoConnector.getWareInfo(id);
		wareInfo.setId(null); // 设置ID为空，则在页面提交时就是新增
		mv.addObject("info", wareInfo);
		mv.addObject("op", "copy");
		return mv;
	}
	
	/**
	 * 保存WareInfoPo对象，无ID则新增，有ID则更新
	 * 
	 * @param wareInfo
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ModelAndView saveWareInfo(WareInfoPo wareInfo) {
		wareInfoConnector.saveWareInfo(wareInfo);
		return getWareInfos(new WareInfoPo(), null);
	}

	/**
	 * 根据ID删除WareInfoPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	public Integer delWareInfo(@PathVariable("id") Integer id) {
		return wareInfoConnector.delWareInfo(id);
	}
	
	/**
	 * 根据ID更新库存信息
	 * 
	 * @param id	商品ID
	 * @param number	更新后的商品库存数量
	 * @Author 迷蝶
	 * @return Integer	本次更新记录数，为1表示成功。
	 */
	@RequestMapping("ni/{id}.html")
	public Integer modifyNumberInventory(@PathVariable("id") Integer id, int number) {
		WareInfoPo wareInfo = new WareInfoPo();
		wareInfo.setId(id);
		wareInfo.setNumberInventory(number);
		return wareInfoConnector.saveWareInfo(wareInfo);
	}
	
	/**
	 * 
	 * 根据ID更新价格信息
	 * 
	 * @param id	商品ID
	 * @param priceOriginal		修改后的原价
	 * @param pricePromotional	修改后的优惠价
	 * @param score						修改后逇积分
	 * @Author 迷蝶
	 * @return Integer	本次更新记录数，为1表示成功。
	 */
	@RequestMapping("price/{id}.html")
	public Integer modifyNumberInventory(@PathVariable("id") Integer id, BigDecimal priceOriginal,
			BigDecimal pricePromotional, int score, int scorePromote) {
		WareInfoPo wareInfo = new WareInfoPo();
		wareInfo.setId(id);
		wareInfo.setPriceOriginal(priceOriginal);
		wareInfo.setPricePromotional(pricePromotional);
		wareInfo.setScoreBuy(score);
		// TODO GXG 修改推广积分
		wareInfo.setScorePromote(scorePromote);
		wareInfo.setScoreTotal(score + scorePromote);
		return wareInfoConnector.saveWareInfo(wareInfo);
	}
	
	/**
	 * 
	 * 商品上架
	 * 
	 * @param id	商品ID
	 * @Author 迷蝶
	 * @return Integer	本次更新记录数，为1表示成功。
	 */
	@RequestMapping("onShelf/{id}.html")
	public Integer onShelf(@PathVariable("id") Integer id) {
		return shelf(id, WareStatus.ONSHELF);
	}
	
	/**
	 * 
	 * 根据ID更新价格信息
	 * 
	 * @param id	商品ID
	 * @param priceOriginal		修改后的原价
	 * @param pricePromotional	修改后的优惠价
	 * @param score						修改后逇积分
	 * @Author 迷蝶
	 * @return Integer	本次更新记录数，为1表示成功。
	 */
	@RequestMapping("offShelf/{id}.html")
	public Integer offShelf(@PathVariable("id") Integer id) {
		return shelf(id, WareStatus.OFFSHELF);
	}
	/**
	 * 上下架操作方法
	 * 
	 * @param id	商品ID
	 * @param status	操作的状态
	 * @Author 迷蝶
	 * @return Integer 本次更新记录数，为1表示成功。
	 */
	private Integer shelf(Integer id, WareStatus status) {
		WareInfoPo wareInfo = new WareInfoPo();
		wareInfo.setId(id);
		wareInfo.setStatus(status.getVal());
		// 设置上下架事件
		wareInfo.setLastShelfTime(new Date());
		return wareInfoConnector.saveWareInfo(wareInfo);
	}
}